<?php
/**
 * Created by PhpStorm.
 * User: jiangtaiping
 * Date: 2017/3/10
 * Time: 10:24
 */
class Service_Activity{

    public static function getActivityList($where=array(),$order=array('create_time'=>'desc'),$page=1){
        $noticeTable = new FTable('activity');
        $list = array(
            'list'=>array(),
            'page_info'=>array()
        );
        /**
         * 如果是前台PC页面获取，或者不是管理员获取
         * 只能查看显示中的活动
         */
        if(!defined('is_admin')){
            $where['status'] = 1;
        }
        if(!isset($where['status'])){
            $where['status']['lt'] = 9;
        }
        $noticeList = $noticeTable->fields('title,create_time,id,status')->page($page)->where($where)->order($order)->limit(10)->select();
        $pagerInfo = $noticeTable->getPagerInfo();
        $list['list']=$noticeList;
        $list['page_info']=$pagerInfo;
        return $list;
    }

    public static function getActivitySignList($where=array(),$order=array('a_s.create_time'=>'desc'),$page=1){
        $noticeTable = new FTable('activity_sign','a_s');
        $list = array(
            'list'=>array(),
            'page_info'=>array()
        );
        $noticeList = $noticeTable->leftJoin('activity','a','a_s.activity_id=a.id')->leftJoin('user','u','a_s.uid=u.uid')->fields('a.title,a_s.create_time,a_s.activity_id,u.truename,a_s.uid,a.status')->page($page)->where($where)->order($order)->limit(10)->select();
        $pagerInfo = $noticeTable->getPagerInfo();
        $list['list']=$noticeList;
        $list['page_info']=$pagerInfo;
        return $list;
    }

    /**
     * 检测用户是否报名参加了指定活动
     * @param int $uid 用户id
     * @param int $activity 活动ID
     * @return int 状态码，1：未报名，2：已报名
     */
    public static function checkSign($uid,$activity_id){
        $cache_key = 'activity_sign_'.$uid.'_'.$activity_id;
        $sign_status = FCache::get($cache_key);
        if(!$sign_status){
            $table = new FTable('activity_sign');
            $check = $table->fields('id')->where(array('uid'=>$uid,'activity_id'=>$activity_id))->find();
            if(!$check){
                $sign_status = 1;
            }else{
                $sign_status = 2;
            }
            FCache::set($cache_key,$sign_status,86400 * 30);
        }
        return $sign_status;
    }

    /**
     * 用户报名指定活动
     * @param int $uid 用户UID
     * @param int $activity_id 活动ID
     * @return bool
     */
    public static function signup($uid,$activity_id){
        $flag = self::checkSign($uid,$activity_id);
        if($flag == 1){
            $table = new FTable('activity_sign');
            $result = $table->insert(
                array(
                    'uid'=>$uid,
                    'activity_id'=>$activity_id,
                    'create_time'=>CURRENT_DATE_TIME,
                    'status'=>1
                )
            );
            if($result){
                FCache::set('activity_sign_'.$uid.'_'.$activity_id,2,86400 * 30);
                return true;
            }
        }
        return false;
    }
}